home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9437 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: 10 Mar 1996 10:08:25 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hv5qpINNp8a@keats.ugrad.cs.ubc.ca>
  8. References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4huctt$arv@sparcserver.lrz-muenchen.de>,
  12. Kurt Watzka <watzka@stat.uni-muenchen.de> wrote:
  13.  >alex@station.net (Alex Chu) writes:
  14.  >
  15.  >
  16.  >>Hi everybody,
  17.  >
  18.  >>I have a question for the following snip C program.
  19.  >
  20.  >>typedef struct item {
  21.  >>  int val;
  22.  >>  struct item *next;
  23.  >>} ITEM, *PITEM;
  24.  >
  25.  >>main()
  26.  >>{
  27.  >>  PITEM head, current;
  28.  >>  head=(PITEM) malloc(sizeof(ITEM));
  29.  >>            ^^^^^^^
  30.  >>  head->val=1;
  31.  >>}
  32.  >
  33.  >>I want to know why need to use the type casting PITEM in front of the
  34.  >>malloc ?  Please help!
  35.  >
  36.  >Simple answer: You don't have to use that cast, and you should _not_ use
  37.  >it, because all you can do with that cast is _hide_ an error.
  38.  
  39. That is true. In fact, in this case there is an error that has been hidden.
  40.  
  41. A use of malloc() requires that the <stdlib.h> header be included. Otherwise,
  42. the compiler will assume the default declaration: ``int malloc();''.
  43.  
  44. The cast in this case does indeed hide an error, because it coerces an int type
  45. into a pointer. 
  46.  
  47. The declaration for malloc() gives it a void * return type, which can, in this
  48. case, be converted to any pointer type (only because malloc() ensures that it
  49. can meet the strictest alignment requirements)
  50.  
  51. assignment-compatible with any pointer.
  52.  
  53.  >OTOH, if you are using a C++ compiler to translate C programs, the cast
  54.  >is needed, but malloc() is obsolete.
  55.  
  56. That is completely false. It is the only standard defined way to allocate
  57. memory. When C++ becomes standardized, malloc() can be considered obsolete and
  58. only from the point of view of C++. It is, however, an integrated, standardized
  59. part of the _C_ language. A C compiler is free to treat it, and other defined
  60. functions, as a special operator for the purposes of optimizing a call to
  61. malloc(), unless it has already encountered a static declaration of the malloc
  62. identifier in the scope where the call occurs---malloc() is no mere external
  63. function, it is a special language feature painstakingly dressed up to look
  64. like a library function, even to the point that you can take the address of
  65. malloc() and call it via a function pointer.
  66. -- 
  67.  
  68.